package com.caac.core.web.action.user;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.caac.core.config.QueryClass;
import com.caac.core.dm.NormalUser;
import com.caac.core.util.Page;
import com.caac.core.web.action.BasePageViewAction; 
import com.caac.core.web.action.PageViewForward;
import com.caac.core.web.form.BaseForm;
import com.caac.core.web.form.UserSearchForm;
import com.caac.core.web.view.UserView;

import org.apache.struts.action.ActionMapping;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright AgileCentury 2005</p>
 * <p>Company: AgileCentury</p>
 * @author <a href="mailto:project@agilecentury.com">Project Group</a>
 * @version 1.0
 */

public class SearchUserAction extends BasePageViewAction {
    private static final String QUERY_NAME = "userQuery";
    private static final String ORDER_NAME = "userOrder";
    private static final String VIEW_NAME = "userView";

    protected PageViewForward doPageViewExecute(ActionMapping mapping,
                                                BaseForm form,
                                                HttpServletRequest request,
                                                HttpServletResponse response,
                                                int pageNo, int pageSize) throws
            java.lang.Exception {
        UserSearchForm myForm = (UserSearchForm) form;
        QueryClass qc = getQuery(QUERY_NAME);
        Page page = new Page(getBaseManager().getCount(NormalUser.class, qc,
                myForm,
                myForm.isAnd()), pageNo, pageSize);

        List dmList = getBaseManager().getObjects(NormalUser.class, qc, myForm,
                                                  myForm.isAnd(),
                                                  getOrder(ORDER_NAME), page);
        List vList = new ArrayList();
        for (int i = 0; i < dmList.size(); i++) {
            NormalUser user = (NormalUser) dmList.get(i);
            UserView userView = new UserView();
            userView.setUser(user);
            vList.add(userView);
        }
        return new PageViewForward(forward(mapping), vList, page,
                                   getSelViewProps(VIEW_NAME), VIEW_NAME,
                                   ORDER_NAME);
    }
}
